var api = require('../../../../api.js');
var utils = require('../../../../utils.js');
// var WxParse = require('../../wxParse/wxParse.js');
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {

    },
    // delete this car_list
    del: function(t) {
        console.log(t)
        console.log("delete")
        var del_id = t.currentTarget.dataset.value;
        app.request({
            url: api.car.del,
            data: {
                id: del_id
            },
            method: "GET",
            success: function(res) {
                console.log(res)
                if (res.code == 0) {
                    wx.showToast({
                        title: '删除成功',
                        icon: 'success',
                        duration: 2500,
                        mask: true,
                        success: function(res) {},
                    })
                    setTimeout(function() {
                        wx.redirectTo({
                            url: '/page/carbarn/pages/car_list/car_list',
                        })
                    }, 1000)
                }
            },
        })
    },


    detail: function(e) {
        var that = this;
        console.log(e)
        let id = e.currentTarget.dataset.id;
        let car_name = e.currentTarget.dataset.car_name;
        let series_id = e.currentTarget.dataset.series_id;
        let model_id = e.currentTarget.dataset.model_id;
        wx.navigateTo({
            url: '/page/carbarn/pages/change/change?id=' + id + '&car_name=' + car_name + '&model_id=' + model_id + '&series_id=' + series_id,
        })
    },
    toadd: function() {
        wx.navigateTo({
            url: '/page/carbarn/pages/carbarn',
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        let that = this;
        app.request({
            url: api.car.my_form,
            data: {},
            method: 'GET',
            dataType: 'json',
            responseType: 'text',
            success: function(res) {
                console.log(res.data)
                var carbarn_list = res.data;
                that.setData({
                    carbarn_list: res.data
                })

                app.request({
                    url: api.car.series,
                    method: 'GET',
                    success: function(res) {
                        console.log(res[0])
                        let car_lsit = res[0];
                        that.setData({
                            _list: res[0]
                        })
                        var series_list = [];
                        for (var i = 0; i < car_lsit.length; i++) {
                            series_list.push(car_lsit[i].id);
                            console.log(car_lsit[i].id);
                        }
                        that.setData({
                            series_list: series_list
                        })
                        // 定义一个空数组  添加到循环中 使用

                        var ser_list = [];
                        for (var j = 0; j < carbarn_list.length; j++) {
                            // console.log(carbarn_list[j].id)
                            for (var i = 0; i < car_lsit.length; i++) {
                                if (car_lsit[i].id == carbarn_list[j].series_id) {
                                    var obj = new Object();
                                    obj.name = car_lsit[i].series_name;
                                    obj.logo = car_lsit[i].cover_pic;
                                    obj.id = carbarn_list[j].id,
                                    obj.plate_num = carbarn_list[j].plate_num;

                                    obj.series_id = carbarn_list[j].series_id;
                                    obj.model_id = carbarn_list[j].model_id,

                                    ser_list.push(obj)
                                }
                            }
                        }
                        console.log(ser_list);
                        // ser_list = ser_list.reverse()
                        that.setData({
                            ser_list: ser_list
                        })
                    },
                })

            },
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {
        let that = this;

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})